//
//  ViewController.m
//  15-homework-Customlayout-wangyi
//
//  Created by 王道林 on 16/8/3.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "ViewController.h"
#import "CustomCollectionViewCell.h"
#import "SelfZiseTool.h"
#import "CustomLayout.h"
#define COLORAUTO [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0]
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, CustomLayoutDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *marrLabel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createData];
    [self createSubview];
}

- (void)createData
{
        _marrLabel = [NSMutableArray arrayWithObjects:@"头条", @"精选精选", @"娱乐", @"体育", @"网易号", @"北京牛逼", @"视频", @"财经", @"科技", @"汽车", @"时尚", @"图片", @"直播", @"热点", @"跟帖", @"轻松一刻", nil];
}


- (void)createSubview
{
    // 创建layout   系统的flow
//    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//    
//    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    layout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
//    layout.minimumLineSpacing = 30;
//    layout.minimumInteritemSpacing = 30;
    
    // 自定义的
    
    CustomLayout *layout = [[CustomLayout alloc] init];
    layout.delegate = self;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [_collectionView registerNib:[UINib nibWithNibName:@"CustomCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"wang"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _marrLabel.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"wang" forIndexPath:indexPath];
    cell.backgroundColor = COLORAUTO;
    cell.label.text = _marrLabel[indexPath.row];
 
    return cell;
}


#if 0
//  系统的laout 方法
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *name = _marrLabel[indexPath.row];
    CGFloat width = [SelfZiseTool selfSizeWithString:name height:0 font:[UIFont systemFontOfSize:17]];

    return CGSizeMake(width, 40);
}


#endif



// 自定义的 layout 代理方法

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout heightForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *name = _marrLabel[indexPath.row];
    CGFloat width = [SelfZiseTool selfSizeWithString:name height:0 font:[UIFont systemFontOfSize:17]];
    
    return width;
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
